package com.dong.es.entity.searchparam.query.range;

import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import co.elastic.clients.json.JsonData;
import com.dong.es.util.DateUtils;

import java.util.Date;

/**
 * @description: Es 时间范围参数
 * @author: 陈瑞东
 * @create: 2022-06-23 11:35
 */
public class EsDataRangeQueryParam extends EsRangeQueryParam<Date> {

    private static final long serialVersionUID = -7804335490128954986L;

    public static final String DATEFORMAT = "yyyy-MM-dd'T'HH:mm:ss'+08:00'";

    public EsDataRangeQueryParam(String field, Date start, Date end) {
        super(field, start, end);
    }

    @Override
    public void loadTo(BoolQuery.Builder target) {
        target.filter(
                filter -> filter.range(
                        range -> range
                                .field(field)
                                .gte(JsonData.of(DateUtils.formatDate(start, DATEFORMAT)))
                                .lt(JsonData.of(DateUtils.formatDate(end, DATEFORMAT)))
                )
        );
    }
}
